<?php

namespace app\api\controller;

use app\admin\model\Organization AS OrganizationMdl;
use app\admin\model\Member;

class Organization extends BaseController
{
    /**
     * 组织列表
     */
    public function getList()
    {
        $param = self::getHttpParam();
        $start = 0;
        $length = 20;
        if (empty($param->townId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'townId不能为空');
        }

        if (!empty($param->start)) {
            $start = $param->start;
        }
        if (!empty($param->length)) {
            $length = $param->length;
        }
        $where['townId'] = $param->townId;
        $where['isDelete'] = 2;
        if(!empty($param->villageId)) {
            $where['villageId'] = $param->villageId;
        }

        $list = OrganizationMdl::where($where)->order('sorting ASC')->limit($start, $length)->select();
        $total = OrganizationMdl::where($where)->count();

        if (empty($list)) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到组织信息');
        }

        return showTotal(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $list, $total);
    }

    /**
     * 组织下的成员列表
     */
    public function partyList()
    {
        $param = self::getHttpParam();
        $start = 0;
        $length = 20;
        if (empty($param->organizationId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'organizationId 不能为空');
        }

        if (!empty($param->start)) {
            $start = $param->start;
        }
        if (!empty($param->length)) {
            $length = $param->length;
        }

        $where = [
            'organizationId' => $param->organizationId,
            'shenheStatus' => ['in', [0,1]],
            'isDelete' => 2
        ];
        $list = Member::where($where)->limit($start, $length)->order('createDate DESC')->select();
        $total = Member::where($where)->count();
        foreach ($list as $k => $v) {
            $list[$k]['avatar'] = $v['avatar'].'?imageView2/1/w/110/h/110';
        }

        if (empty($list)) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到成员信息');
        }

        return showTotal(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $list, $total);
    }

}